package com.demo.servlet.demo1213;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/demo1213/demo01.do")
public class Demo01Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过响应对象的字符输出流 回传HTML页面
        //1.设置响应的内容类型为
        response.setContentType("text/html;charset=utf-8");
        //2.获取响应字符输出流
        PrintWriter writer = response.getWriter();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //3.写入HTML字符串
        writer.write("<!DOCTYPE html>");
        writer.write("<html lang=\"en\">");
        writer.write("<head>");
        writer.write("    <meta charset=\"UTF-8\" http-equiv=\"refresh\" content=\"1\">");
        writer.write("    <title>计时HTML</title>");
        writer.write("</head>");
        writer.write("<body>");
        writer.write("<h1>" + sdf.format(new Date()) + "</h1>");
        writer.write("</body>");
        writer.write("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
